package com.local.videoplayer.utils;

import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by an on 2017/5/28.<br/>
 */

public class SpanHelper {
  private final List<SpanBuilder> mSpanBuilders = new ArrayList<>();
  private TextView mTextView;

  public static SpanHelper newInstance() {
    return new SpanHelper();
  }

  public static SpanHelper newInstance(TextView textView) {
    SpanHelper spanHelper = new SpanHelper();
    spanHelper.mTextView = textView;
    return spanHelper;
  }

  public SpanHelper clear() {
    mTextView.setText("");
    return this;
  }

  public SpanHelper setText(CharSequence text) {
    mTextView.setText(text);
    return this;
  }

  public SpanHelper append(SpanBuilder builder) {
    mSpanBuilders.add(builder);
    return this;
  }

  public SpanHelper append(CharSequence text) {
    return append(SpanBuilder.span(text));
  }

  public SpanHelper click() {
    mTextView.setMovementMethod(LinkMovementMethod.getInstance());
    return this;
  }

  public SpannableStringBuilder build() {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    for (SpanBuilder spanBuilder : mSpanBuilders) {
      builder.append(spanBuilder.build());
    }

    return builder;
  }

  public TextView commit() {
    mTextView.setText(build());
    return mTextView;
  }
}
